package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.MarketBrand;
import com.cskaoyan.bean.VoCCY.*;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.AdminRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;


@RestController
@RequestMapping("admin")
@ResponseBody
public class AdminRegionController {
    @Autowired
    AdminRegionService adminRegionService;

    @RequestMapping("region/list")
    public BaseRespVo list() {
        CommonData data = adminRegionService.query();

        return BaseRespVo.ok(data);
    }

    @RequestMapping("brand/list")
    public BaseRespVo brandList(Integer page,Integer limit,String id,String name){

        CommonData data = adminRegionService.brandListQuery(page,limit,id,name);

        return BaseRespVo.ok(data);
    }

    @RequestMapping("brand/create")
    public BaseRespVo brandCreate(@RequestBody MarketBrandCreateBo brandCreateBo) throws ParseException {
        String name = brandCreateBo.getName();
        String desc = brandCreateBo.getDesc();
        double floorPrice = brandCreateBo.getFloorPrice();
        String picUrl = brandCreateBo.getPicUrl();

        MarketBrandCreateVo marketBrandCreateVo = adminRegionService.insertBrand(name,desc,floorPrice,picUrl);

        return BaseRespVo.ok(marketBrandCreateVo);
    }

    @RequestMapping("brand/update")
    public BaseRespVo brandUpdate(@RequestBody MarketBrandUpdateVo brandUpdateVo) {
        MarketBrandUpdateVo marketBrandUpdateVo = adminRegionService.updateBrand(brandUpdateVo);

        return BaseRespVo.ok(marketBrandUpdateVo);
    }

    @RequestMapping("brand/delete")
    public BaseRespVo brandDelete(@RequestBody MarketBrand brandUpdateVo) {
        adminRegionService.deleteBrandDelete(brandUpdateVo.getId());

        return BaseRespVo.ok3();
    }

    @RequestMapping("category/list")
    public BaseRespVo categoryList() {
        CommonData data = adminRegionService.queryCategoryList();

        return BaseRespVo.ok(data);
    }

    @RequestMapping("category/l1")
    public BaseRespVo categoryL1() {
        CommonData data = adminRegionService.queryCategoryL1();

        return BaseRespVo.ok(data);
    }

    @RequestMapping("category/create")
    public BaseRespVo categoryCreate(@RequestBody MarketCategoryCreateBo createBo) {
        MarketCategoryCreateVo categoryCreateVo = adminRegionService.insertCategory(createBo);

        return BaseRespVo.ok(categoryCreateVo);
    }

    @RequestMapping("category/update")
    public BaseRespVo categoryUpdate(@RequestBody MarketCategoryListVo categoryListVo) {
        adminRegionService.updateCategory(categoryListVo);

        return BaseRespVo.ok3();
    }

    @RequestMapping("category/delete")
    public BaseRespVo categoryDelete(@RequestBody MarketCategoryListVo categoryListVo) {

        List<MarketCategoryListVoVo> children = categoryListVo.getChildren();

        if (children == null) {
            Integer id = categoryListVo.getId();
            adminRegionService.deleteCategory(id);
            return BaseRespVo.ok3();

        }
        Integer id = categoryListVo.getId();

        if (children.size() != 0) {
            return BaseRespVo.invalidParameter("该一级类目下面有二级目录，暂时不支持删除");
        }

        adminRegionService.deleteCategory(id);
        return BaseRespVo.ok3();
    }

    @RequestMapping("log/list")
    public BaseRespVo logList(Integer page,Integer limit,String name) {
        CommonData data = adminRegionService.queryLogList(page,limit,name);

        return BaseRespVo.ok(data);
    }

}
